<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Expenses extends CI_Controller {

    public function index() {
        if (!$this->session->userdata('logged_in')) {
            redirect('/users/login/');
        }
    }

    public function add() {
        if (!$this->session->userdata('logged_in')) {
            redirect('/users/login/');
        }

        $user_id = $this->session->userdata('user_id');
        $this->load->model('Expense');

        if ($this->input->post('submit')) {
            $items_name = $this->input->post('name');
            $items_amount = $this->input->post('amount');
            $items_token = $this->input->post('token');

            for ($i = 0; $i < count($items_name); ++$i) {
                $name = $items_name[$i];
                $amount = $items_amount[$i];
                $token = $items_token[$i];

                if (($name != '') && ($amount != '')) {
                    $expense = array(
                        'user_id' => $user_id,
                        'name' => $name,
                        'amount' => $amount,
                        'token' => $token
                    );

                    $this->Expense->add($expense);
                }
            }

            $this->session->set_flashdata('warning', 'Your expense has been saved. <a href="' . site_url('/expenses/view/' . $user_id) . '">View All</a>');
            redirect('/expenses/add/', 'refresh');
        } else {
            $data = array(
                'title' => 'Add'
            );

            $this->load->view('header', $data);

            mt_srand();
            $random = mt_rand();
            $token = md5($random);

            $user_id = $this->session->userdata('user_id');
            $total = $this->Expense->get_total($user_id);
            $today = $this->Expense->get_today($user_id);

            $data = array(
                'token' => $token,
                'today' => $today,
                'total' => $total
            );

            $this->load->view('expenses_add', $data);
            $this->load->view('footer');
        }
    }

    public function view($user_id, $offset = 0) {
        $this->load->library('table');
        $this->load->library('pagination');
        $this->load->model('Expense');
        $this->load->model('User');

        $user = $this->User->get($user_id);
        $username = $user->name;

        $expenses = $this->Expense->get($user_id, $offset);
        $headers = array('Name', 'Amount', 'Timestamp');
        array_unshift($expenses, $headers);

        $table = $this->table->generate($expenses);

        $config['uri_segment'] = 4;
        $config['base_url'] = site_url('/expenses/view/' . $user_id);
        $config['total_rows'] = $this->Expense->count_all($user_id);
        $config['per_page'] = '10';

        $this->pagination->initialize($config);

        $total = $this->Expense->get_total($user_id);
        $today = $this->Expense->get_today($user_id);

        $data = array(
            'title' => $username . '\'s Expenses'
        );

        $this->load->view('header', $data);

        $data = array(
            'title' => $username,
            'user_id' => $user_id,
            'total' => $total,
            'today' => $today,
            'table' => $table,
            'pagination' => $this->pagination->create_links()
        );

        $this->load->view('expenses_view', $data);
        $this->load->view('footer');
    }

    public function chart() {
        $this->load->model('User');
        $this->load->model('Expense');

        $users = $this->User->get_all();

        $dataset = array();

        foreach ($users as $user) {
            $summary = $this->Expense->summary($user['id']);
            $dataset[$user['id']] = $summary;
        }

        $data = array(
            'data' => $dataset
        );

        $this->load->view('chart', $data);
    }

    public function graph() {
        $data = array(
            'title' => 'Graph'
        );

        $this->load->view('header', $data);
        $this->load->view('graph');
        $this->load->view('footer', $data);
    }

}
